package com.qys.training.biz.sysconfig.service.impl;

import com.qys.training.base.enumerate.BizCodeEnum;
import com.qys.training.base.exception.QysException;
import com.qys.training.biz.cache.service.CacheService;
import com.qys.training.biz.sysconfig.mapper.SysConfigMapper;
import com.qys.training.biz.sysconfig.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Winstone
 * @date 2020/8/5 - 2:21 下午
 */
@Service
public class SysConfigServiceImpl implements SysConfigService {

    @Autowired
    CacheService cacheService;
    @Autowired
    SysConfigMapper sysConfigMapper;

    @Value("${hazelcast.config.sys_config.ttl}")
    private Long ttl;

    @Value("${hazelcast.config.sys_config.tableName}")
    private String tableName;


    /**
     * 查询是否有配置
     *
     * @param key
     * @return
     */
    public List<String> getConfigValue(String key) {
        List<String> value = cacheService.getValue(tableName, key);
        List<String> configValue;
        if (value == null) {
            configValue = sysConfigMapper.selectByConfigKey(key);
            if (configValue == null){
                throw new QysException(BizCodeEnum.CONFIGURATION_ITEM_DOES_NOT_EXIST.getCode(), BizCodeEnum.CONFIGURATION_ITEM_DOES_NOT_EXIST.getDescription());
            }else {
                cacheService.put(tableName,key,configValue,ttl);
            }
        }else {
            configValue = cacheService.getValue(tableName,key);
        }
        return configValue;
    }
}
